//
//  FxTabBar.m
//  TodayComment
//
//  Created by 张吉阳 on 15/10/7.
//  Copyright (c) 2015年 xianrenmoshi. All rights reserved.
//

#import "FxTabBar.h"
#import "UIColor+SNFoundation.h"
@implementation FxTabBar
- (instancetype)initWithFrame:(CGRect)frame buttonImages:(NSArray *)imageArray
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake([[UIScreen mainScreen]bounds].origin.y, [[UIScreen mainScreen]bounds].size.height-frame.size.height, [[UIScreen mainScreen] bounds].size.width , frame.size.height);
        self.backgroundColor = [UIColor colorWithHexString:@"f0f0f0"];
        _backgroundView  =[[UIImageView alloc]initWithFrame:self.bounds];
        //self.userInteractionEnabled = NO;
        //_backgroundView.userInteractionEnabled = NO;
        [self addSubview:_backgroundView];
        _buttons = [NSMutableArray arrayWithCapacity:imageArray.count];
        UIButton *button = nil;
        CGFloat width = [[UIScreen mainScreen] bounds].size.width / [imageArray count];
        for (int i = 0; i < imageArray.count; i++) {
            button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.showsTouchWhenHighlighted = YES;
            button.tag = i;
            button.frame = CGRectMake(width * i, 0, width, frame.size.height);
            [button setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
            [_buttons addObject:button];
            [self addSubview:button];
        }
    }
    return self;
}

- (void)setBackgroundImage:(UIImage *)img
{
    [_backgroundView setImage:img];
}

- (void)tabBarButtonClicked:(UIButton *)sender
{
    UIButton *button = sender;
    if ([_delegate respondsToSelector:@selector(tabBar:didSelectIndex:)]) {
        [_delegate tabBar:self didSelectIndex:button.tag];
    }
}

- (void)selectTabAtIndex:(NSInteger)index
{
    for (int i = 0; i < _buttons.count; i++) {
        UIButton *button = [_buttons objectAtIndex:i];
        button.selected = NO;
        button.userInteractionEnabled = YES;
    }
    UIButton *btn = [_buttons objectAtIndex:index];
    btn.selected = YES;
    btn.userInteractionEnabled = NO;
}

- (void)removeTabAtIndex:(NSInteger)index
{
    [[_buttons objectAtIndex:index] removeFromSuperview];
    [_buttons removeObjectAtIndex:index];
    CGFloat width = 320.0f / _buttons.count;
    for (UIButton *button in _buttons) {
        if (button.tag > index) {
            button.tag --;
        }
        button.frame = CGRectMake(width * button.tag, 0, width, self.frame.size.height);
    }
}

- (void)insertTabWithImageDic:(NSDictionary *)dict atIndex:(NSUInteger)index
{
    CGFloat width = 320.0f / ([self.buttons count] + 1);
    for (UIButton *b in self.buttons)
    {
        if (b.tag >= index)
        {
            b.tag ++;
        }
        b.frame = CGRectMake(width * b.tag, 0, width, self.frame.size.height);
    }
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.showsTouchWhenHighlighted = YES;
    btn.tag = index;
    btn.frame = CGRectMake(width * index, 0, width, self.frame.size.height);
    [btn setImage:[dict objectForKey:@"Default"] forState:UIControlStateNormal];
    [btn setImage:[dict objectForKey:@"Highlighted"] forState:UIControlStateHighlighted];
    [btn setImage:[dict objectForKey:@"Seleted"] forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttons insertObject:btn atIndex:index];
    [self addSubview:btn];
}



@end
